package com.aruba.consumer.service.impl;

import com.aruba.bean.User;
import com.aruba.consumer.service.UserService;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public List<User> getUserList() throws IOException, InterruptedException, KeeperException, NotBoundException {
        ZooKeeper zooKeeper = new ZooKeeper("192.168.42.4:2181", 10000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

            }
        });
        // 通过zookeeper获取rmi的url
        byte[] url = zooKeeper.getData("/demos/userService", false, null);

        // 获取rmi的远程对象
        com.aruba.rmi.service.UserService userService = (com.aruba.rmi.service.UserService) Naming.lookup(new String(url));

        return userService.getUserList();
    }
}
